package wishlistAction;

import java.util.ArrayList;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import productModel.Books;
import productModel.Computers;
import productModel.Gaming;
import productModel.Phones;
import productModel.ProductEAV;
import productModel.ProductItem;

import wishlistModel.Wishlist;

public class showWishlist {

	Wishlist wishlist;
	ArrayList<ProductItem> productList;
	String sortOption;
	int accessSpecifier;
	String publicID;
	String customerName;
	int productID;
	Map session;
	
	//--------------------------methods for logged-in user's wishlist: --------------------------------//
	
	public String displayWishlist() {
		wishlist = Wishlist.getWishlistByUserId(3);			//softcode it(sagar)
		wishlist.setWishlistItemIds(Wishlist.getWishlistItems(wishlist.getWishlistId()));
		productList = new ArrayList<ProductItem>();
		session = ActionContext.getContext().getSession();
		session.put("wishlistID", wishlist.getWishlistId());
		
		for(int i=0; i<wishlist.getWishlistItemIds().size(); i++){
			
			int tempProductId = wishlist.getWishlistItemIds().get(i);
			ArrayList<ProductEAV> tempProductDetails = ProductEAV.getItemDetails(tempProductId);
			String tempProductName = ProductItem.getProductNamebyProductId(tempProductId);
			String tempProductCategory = ProductItem.getProductCategorybyProductEAV(tempProductDetails);
			
			if(tempProductCategory.equalsIgnoreCase("Phones")){
				Phones phone = new Phones();
				phone.setProductId(tempProductId);
				phone.setProductName(tempProductName);
				phone.getProductDetailsFromEAV(tempProductDetails);
			//	phone.showDetails();
				productList.add(phone);
			}
			
			else if(tempProductCategory.equalsIgnoreCase("Books")){
				Books book = new Books();
				book.setProductId(tempProductId);
				book.setProductName(tempProductName);
				book.getProductDetailsFromEAV(tempProductDetails);
			//	book.showDetails();
				productList.add(book);
			}
			
			else if(tempProductCategory.equalsIgnoreCase("Computers")){
				Computers computer = new Computers();
				computer.setProductId(tempProductId);
				computer.setProductName(tempProductName);
				computer.getProductDetailsFromEAV(tempProductDetails);
			//	computer.showDetails();
				productList.add(computer);
			}
			
			else if(tempProductCategory.equalsIgnoreCase("Gaming")){
				Gaming game = new Gaming();
				game.setProductId(tempProductId);
				game.setProductName(tempProductName);
				game.getProductDetailsFromEAV(tempProductDetails);
			//	game.showDetails();
				productList.add(game);
			}
		}
		return "success";
	}
	
	public String sortWishlist(){
		this.displayWishlist();
		ArrayList<ProductItem> tempList = null;
//		System.out.println("selected: " + sortOption);
		try{
			if(sortOption.equals("dd")){
				tempList = new ArrayList<ProductItem>();
				for(int i=productList.size()-1; i>=0; i--)
					tempList.add(productList.get(i));
				productList = tempList;
			}
			else if(sortOption.equals("pa")){
				productList = Wishlist.sortByPrice(productList, "asc");
			}
			else if(sortOption.equals("pd")){
				productList = Wishlist.sortByPrice(productList, "desc");
			}
		}catch(NullPointerException ex){
			System.out.println("Exception caught: possible problem: sortOption value might be null");
		}
		return "success";
	}
	
	public String changeWishlistAccess(){
		session = ActionContext.getContext().getSession();
		Wishlist.changeWishlistAccess(Integer.parseInt(session.get("wishlistID").toString()), this.getAccessSpecifier());
		this.displayWishlist();
		return "success";
	}
	
	public String removeItemFromWishlist(){
//		System.out.println(this.getProductID());
		session = ActionContext.getContext().getSession();
		Wishlist.removeItemFromWishlist(Integer.parseInt(session.get("wishlistID").toString()), this.getProductID());
		this.displayWishlist();
		return "success";
	}
	
	//------------------methods for someone's public wishlist: --------------------------------//
	
	public String displayPublicWishlist(){
//		System.out.println(this.getPublicID());
		session = ActionContext.getContext().getSession();
		session.put("publicID", this.getPublicID());
		customerName = Wishlist.getCustomerName(this.getPublicID());
		if(Wishlist.isPublicViewAllowed(this.getPublicID())){
			wishlist = Wishlist.getWishlistByPublicLink(publicID);
			wishlist.setWishlistItemIds(Wishlist.getWishlistItems(wishlist.getWishlistId()));
			productList = new ArrayList<ProductItem>();
			
			for(int i=0; i<wishlist.getWishlistItemIds().size(); i++){
				
				int tempProductId = wishlist.getWishlistItemIds().get(i);
				ArrayList<ProductEAV> tempProductDetails = ProductEAV.getItemDetails(tempProductId);
				String tempProductName = ProductItem.getProductNamebyProductId(tempProductId);
				String tempProductCategory = ProductItem.getProductCategorybyProductEAV(tempProductDetails);
				
				if(tempProductCategory.equalsIgnoreCase("Phones")){
					Phones phone = new Phones();
					phone.setProductId(tempProductId);
					phone.setProductName(tempProductName);
					phone.getProductDetailsFromEAV(tempProductDetails);
				//	phone.showDetails();
					productList.add(phone);
				}
				
				else if(tempProductCategory.equalsIgnoreCase("Books")){
					Books book = new Books();
					book.setProductId(tempProductId);
					book.setProductName(tempProductName);
					book.getProductDetailsFromEAV(tempProductDetails);
				//	book.showDetails();
					productList.add(book);
				}
				
				else if(tempProductCategory.equalsIgnoreCase("Computers")){
					Computers computer = new Computers();
					computer.setProductId(tempProductId);
					computer.setProductName(tempProductName);
					computer.getProductDetailsFromEAV(tempProductDetails);
				//	computer.showDetails();
					productList.add(computer);
				}
				
				else if(tempProductCategory.equalsIgnoreCase("Gaming")){
					Gaming game = new Gaming();
					game.setProductId(tempProductId);
					game.setProductName(tempProductName);
					game.getProductDetailsFromEAV(tempProductDetails);
				//	game.showDetails();
					productList.add(game);
				}
			}
			return "success";
		}
		else{
			return "failure";
		}
	}
	
	public String sortPublicWishlist(){
		session = ActionContext.getContext().getSession();
		this.setPublicID(session.get("publicID").toString());
		this.displayPublicWishlist();
		ArrayList<ProductItem> tempList = null;
//		System.out.println("selected: " + sortOption);
		try{
			if(sortOption.equals("dd")){
				tempList = new ArrayList<ProductItem>();
				for(int i=productList.size()-1; i>=0; i--)
					tempList.add(productList.get(i));
				productList = tempList;
			}
			else if(sortOption.equals("pa")){
				productList = Wishlist.sortByPrice(productList, "asc");
			}
			else if(sortOption.equals("pd")){
				productList = Wishlist.sortByPrice(productList, "desc");
			}
		}catch(NullPointerException ex){
			System.out.println("Exception caught: possible problem: sortOption and/or publicID value(s) might be null");
		}
		return "success";
	}

	public Wishlist getWishlist() {
		return wishlist;
	}

	public void setWishlist(Wishlist wishlist) {
		this.wishlist = wishlist;
	}

	public ArrayList<ProductItem> getProductList() {
		return productList;
	}

	public void setProductList(ArrayList<ProductItem> productList) {
		this.productList = productList;
	}

	public String getSortOption() {
		return sortOption;
	}

	public void setSortOption(String sortOption) {
		this.sortOption = sortOption;
	}

	public int getAccessSpecifier() {
		return accessSpecifier;
	}

	public void setAccessSpecifier(int accessSpecifier) {
		this.accessSpecifier = accessSpecifier;
	}

	public String getPublicID() {
		return publicID;
	}

	public void setPublicID(String publicID) {
		this.publicID = publicID;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public int getProductID() {
		return productID;
	}

	public void setProductID(int productID) {
		this.productID = productID;
	}

}
